package com.yztc.orangeentertainment.ui.main.subscribefragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.yztc.orangeentertainment.R;
import com.yztc.orangeentertainment.adapter.StarViewPagerAdapter;
import com.yztc.orangeentertainment.adapter.SubInterestAdapter;
import com.yztc.orangeentertainment.base.BaseFragment;
import com.yztc.orangeentertainment.base.OnItemClickListener;
import com.yztc.orangeentertainment.bean.subscribebean.ListBean;
import com.yztc.orangeentertainment.config.SubscribeUrlConfig;
import com.yztc.orangeentertainment.ui.main.channalarticle.ChannalArticleActivity;
import com.yztc.orangeentertainment.ui.main.subscribefragment.starfragment.StarFragment;
import com.yztc.orangeentertainment.ui.main.subscribefragment.starlist.StarListActivity;
import com.yztc.orangeentertainment.ui.main.subscribefragment.subconfragment.SubConFragement;
import com.yztc.orangeentertainment.ui.main.subscribefragment.sublablist.LabListActivity;
import com.yztc.orangeentertainment.utils.DividerItemDecoration;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by My on 2016/10/5.
 * 订阅fragment
 */
public class SubscribeFragment extends BaseFragment implements SubContract.View, View.OnClickListener {
    @InjectView(R.id.iv_sub_star_search)
    ImageView ivSubStarSearch;
    @InjectView(R.id.vp_sub_star)
    ViewPager mStarViewPager;
    @InjectView(R.id.iv_sub_con_search)
    ImageView ivSubConSearch;
    @InjectView(R.id.vp_sub_con)
    ViewPager mConViewPager;
    @InjectView(R.id.rv_sub_interest)
    RecyclerView mRecyclerView;
    @InjectView(R.id.con)
    LinearLayout con;
    @InjectView(R.id.con2)
    LinearLayout con2;
    private SubPresenter presenter;
    //订阅明星fragment集合
    private List<Fragment> topFragments, middleFragments;
    //viewpager指示性图标
    private ImageView[] icons1, icons2;
    private int page = 1;
    private int lastVisibleItem;
    private StarFragment starFragment1, starFragment2, starFragment3;
    private SubConFragement subConFragment1, subConFragment2, subConFragment3;
    private SubInterestAdapter adapter;
    private ArrayList<ListBean> totalList=new ArrayList<>();
    private LinearLayoutManager linearLayoutManager;
    @Override
    public int getLayoutId() {
        return R.layout.fragment_sub;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.inject(this, rootView);
        initView();
        initPresenter();
        initStarViewPager();
        initConViewPager();
        return rootView;
    }

    /**
     * 初始化视图
     */
    private void initView() {
        ivSubStarSearch.setOnClickListener(this);
        ivSubConSearch.setOnClickListener(this);
        adapter=new SubInterestAdapter(getActivity());
        //mRecyclerView管理器
        linearLayoutManager = new LinearLayoutManager(getActivity()) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
        //水平摆放
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        //间距
        mRecyclerView.addItemDecoration(new DividerItemDecoration(5));
        mRecyclerView.setLayoutManager(linearLayoutManager);
        //上拉加载
        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
            }

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if(newState == RecyclerView.SCROLL_STATE_IDLE && (lastVisibleItem + 1) ==adapter
                        .getItemCount()){
                    page++;
                    initPresenter();
                    adapter.notifyDataSetChanged();
                }
            }
        });
    }


    /**
     * 订阅内容viewpager
     */
    private void initConViewPager() {
        middleFragments = new ArrayList<>();
        //传入第一个fragment数据
        Bundle bundle = new Bundle();
        bundle.putInt("index", 1);
        subConFragment1 = new SubConFragement();
        subConFragment1.setArguments(bundle);
        //传入第二个fragment数据
        bundle = new Bundle();
        bundle.putInt("index", 2);
        subConFragment2 = new SubConFragement();
        subConFragment2.setArguments(bundle);
        //传入第三个fragment数据
        bundle = new Bundle();
        bundle.putInt("index", 3);
        subConFragment3 = new SubConFragement();
        subConFragment3.setArguments(bundle);
        //将fragment加入topFragments集合中
        middleFragments.add(subConFragment1);
        middleFragments.add(subConFragment2);
        middleFragments.add(subConFragment3);

        //加载指示圆点
        icons2 = new ImageView[middleFragments.size()];
        for (int i = 0; i < middleFragments.size(); i++) {
            icons2[i] = new ImageView(getActivity());
            icons2[i].setImageResource(R.mipmap.home_point);
            icons2[i].setLayoutParams(new ViewGroup.LayoutParams(ViewPager.LayoutParams.WRAP_CONTENT,
                    ViewPager.LayoutParams.WRAP_CONTENT));
            icons2[i].setMaxWidth(25);
            icons2[i].setAdjustViewBounds(true);
            //将参数中指定的view添加到layout布局中
            con2.addView(icons2[i]);
        }
        //默认第一个选中
        icons2[0].setImageResource(R.mipmap.home_point_show);

        //viewpager加载adaptrer
        mConViewPager.setAdapter(new StarViewPagerAdapter(getFragmentManager(), middleFragments));
        //viewpager滑动监听
        mConViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                for (int i = 0; i < middleFragments.size(); i++) {
                    icons2[i].setImageResource(R.mipmap.home_point);
                }
                icons2[position].setImageResource(R.mipmap.home_point_show);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }


    /**
     * 初始化订阅明星viewpager
     */
    private void initStarViewPager() {
        topFragments = new ArrayList<>();
        //传入第一个fragment数据
        Bundle bundle = new Bundle();
        bundle.putInt("index", 1);
        starFragment1 = new StarFragment();
        starFragment1.setArguments(bundle);
        //传入第二个fragment数据
        bundle = new Bundle();
        bundle.putInt("index", 2);
        starFragment2 = new StarFragment();
        starFragment2.setArguments(bundle);
        //传入第三个fragment数据
        bundle = new Bundle();
        bundle.putInt("index", 3);
        starFragment3 = new StarFragment();
        starFragment3.setArguments(bundle);
        //将fragment加入topFragments集合中
        topFragments.add(starFragment1);
        topFragments.add(starFragment2);
        topFragments.add(starFragment3);

        //加载指示圆点
        icons1 = new ImageView[topFragments.size()];
        for (int i = 0; i < topFragments.size(); i++) {
            icons1[i] = new ImageView(getActivity());
            icons1[i].setImageResource(R.mipmap.home_point);
            icons1[i].setLayoutParams(new ViewGroup.LayoutParams(ViewPager.LayoutParams.WRAP_CONTENT,
                    ViewPager.LayoutParams.WRAP_CONTENT));
            icons1[i].setMaxWidth(25);
            icons1[i].setAdjustViewBounds(true);
            //将参数中指定的view添加到layout布局中
            con.addView(icons1[i]);
        }
        //默认第一个选中
        icons1[0].setImageResource(R.mipmap.home_point_show);

        //viewpager加载adaptrer
        mStarViewPager.setAdapter(new StarViewPagerAdapter(getFragmentManager(), topFragments));
        //viewpager滑动监听
        mStarViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                for (int i = 0; i < topFragments.size(); i++) {
                    icons1[i].setImageResource(R.mipmap.home_point);
                }
                icons1[position].setImageResource(R.mipmap.home_point_show);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    //

    /**
     * 初始化猜你喜欢presenter
     */
    private void initPresenter() {
        presenter = new SubPresenter(this);
        Map<String, String> map = new HashMap<>();
        //参数map
        map.put(SubscribeUrlConfig.Params.PAGE, page + "");
        map.put(SubscribeUrlConfig.Params.TS, SubscribeUrlConfig.All_DefaultVaule.TS_VALUE);
        map.put(SubscribeUrlConfig.Params.ISLOGIN, SubscribeUrlConfig.All_DefaultVaule.ISLOGIN_VALUE);
        map.put(SubscribeUrlConfig.Params.MAC, SubscribeUrlConfig.All_DefaultVaule.MAC_VALUE);
        map.put(SubscribeUrlConfig.Params.UID, SubscribeUrlConfig.All_DefaultVaule.UID_VALUE);
        map.put(SubscribeUrlConfig.Params.PF, SubscribeUrlConfig.All_DefaultVaule.PF_VALUE);
        map.put(SubscribeUrlConfig.Params.NET, SubscribeUrlConfig.All_DefaultVaule.NET_VALUE);
        map.put(SubscribeUrlConfig.Params.ACCESSTOKEN, SubscribeUrlConfig.All_DefaultVaule.ACCESSTOKEN_VALUE);
        map.put(SubscribeUrlConfig.Params.CHANNEL, SubscribeUrlConfig.All_DefaultVaule.CHANNEL_VALUE);
        map.put(SubscribeUrlConfig.Params.VER, SubscribeUrlConfig.All_DefaultVaule.VER_VALUE);
        map.put(SubscribeUrlConfig.Params.RES, SubscribeUrlConfig.All_DefaultVaule.RES_VALUE);
        presenter.getInterestFromNet(map, getActivity());
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }


    @Override
    public void sendToAdapter(SubInterestAdapter adapter) {

    }

    /**
     * 获取猜你喜欢list
     * @param listBeanList
     */
    @Override
    public void onLoadInterestSuccess(List<ListBean> listBeanList) {
        //加载分页
        totalList.addAll(listBeanList);
        adapter.setList(totalList);
        if (page==1){
            mRecyclerView.setAdapter(adapter);
        }else {
            adapter.notifyDataSetChanged();
        }
        //点击事件
        adapter.setListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent=new Intent(getActivity(), ChannalArticleActivity.class);
                intent.putExtra("id",totalList.get(position).getId()+"");
                startActivity(intent);
            }

            @Override
            public void onItemLongClick(View view, int position) {

            }
        });

    }

    @Override
    public void onLoadInterestFail(String msg) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_sub_star_search:
                //搜索明星
                Intent intent = new Intent(getActivity(), StarListActivity.class);
                startActivity(intent);
                break;
            case R.id.iv_sub_con_search:
                //搜索内容
                Intent intent2 = new Intent(getActivity(), LabListActivity.class);
                startActivity(intent2);
                break;
        }
    }
}
